package udp.message;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class UdpMessage implements Serializable {

	static final long serialVersionUID = 16L;
	
	private int bytesToWrite;
	protected String destHost;
	protected int destPort;
    private byte[] packet;
    private int segmentId; // not really necessary
    protected int srcPort;
    protected String srcHost;
    private int sequenceNumber;
    
    private int socketPort;
	
	public UdpMessage(){
		packet = new byte[1024];
	}
	
	
	public int getSocketPort() {
		return socketPort;
	}



	public void setSocketPort(int socketPort) {
		this.socketPort = socketPort;
	}



	public UdpMessage(byte[] packet, int segmentId, int bytesToWrite, int srcPort){
		this.packet = packet;
		this.segmentId = segmentId;
		this.bytesToWrite = bytesToWrite;
		this.srcPort = srcPort;
		this.sequenceNumber=0;
	}

	public int getSegmentId() {
		return segmentId;
	}

	public void setSegmentId(int segmentId) {
		this.segmentId = segmentId;
	}

	public byte[] getPacket() {
		return packet;
	}

	public void setPacket(byte[] packet) {
		this.packet = packet;
	}

	public int getBytesToWrite() {
		return bytesToWrite;
	}

	public void setBytesToWrite(int bytesToWrite) {
		this.bytesToWrite = bytesToWrite;
	}
	
	public int getSrcPort() {
		return srcPort;
	}

	public void setSrcPort(int srcPort) {
		this.srcPort = srcPort;
	}

	public String getSrcHost() {
		return srcHost;
	}

	public void setSrcHost(String srcHost) {
		this.srcHost = srcHost;
	}

	public int getDestPort() {
		return destPort;
	}

	public void setDestPort(int destPort) {
		this.destPort = destPort;
	}

	public String getDestHost() {
		return destHost;
	}

	public void setDestHost(String destHost) {
		this.destHost = destHost;
	}

	
	public int getSequenceNumber() {
		return sequenceNumber;
	}

	public void setSequenceNumber(int sequenceNumber) {
		this.sequenceNumber = sequenceNumber;
	}

	@Deprecated
	public byte[] getSerializedMessage() throws IOException {
		return serialize(this);
	}
	
	@Deprecated
	public byte[] serialize(Object obj) throws IOException {
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
        objectStream.writeObject(obj);
        objectStream.flush();
        return byteStream.toByteArray();
    }
	
}
